/*
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores
 * CA 94065 USA or visit www.oracle.com if you need additional information or
 * have any questions.
 */

package com.codename1.designer;

import com.codename1.designer.ResourceEditorView;
import com.codename1.ui.util.EditableResources;
import java.util.Arrays;
import javax.swing.DefaultComboBoxModel;

/**
 * Used by the hashtable editor to edit a sepcific entry.
 *
 * @author Shai Almog
 */
public class HashtableKeyValueEditor extends javax.swing.JPanel {
    private EditableResources res;

    /** Creates new form HashtableKeyValueEditor */
    public HashtableKeyValueEditor(EditableResources res, String keyString, Object value) {
        initComponents();
        this.res = res;
        ResourceEditorView.initImagesComboBox(imageValue, res, true, false);
        String[] arr = new String[res.getUIResourceNames().length];
        System.arraycopy(res.getUIResourceNames(), 0, arr, 0, arr.length);
        Arrays.sort(arr);
        navigationCombo.setModel(new DefaultComboBoxModel(arr));
        if(keyString != null) {
            key.setText(keyString);
            if(value instanceof String) {
                if(keyString.equals("$navigation")) {
                    isNavigation.setSelected(true);
                    stringValue.setEnabled(false);
                    key.setEnabled(false);
                    navigationCombo.setEnabled(true);
                    navigationCombo.setSelectedItem(value);
                }
                if(value.equals("true") || value.equals("false")) {
                    booleanValue.setSelected(true);
                    booleanValue.setEnabled(true);
                    isBoolean.setSelected(true);
                    stringValue.setEnabled(false);
                } else {
                    stringValue.setText((String)value);
                }
                return;
            }
            if(value instanceof com.codename1.ui.Image) {
                isImage.setSelected(true);
                imageValue.setSelectedItem(res.findId(value));
                stringValue.setEnabled(false);
                imageValue.setEnabled(true);
                return;
            }
        }
    }

    public String getKey() {
        return key.getText();
    }

    public Object getValue() {
        if(isString.isSelected()) {
            return stringValue.getText();
        }
        if(isBoolean.isSelected()) {
            return "" + booleanValue.isSelected();
        }
        if(isNavigation.isSelected()) {
            return navigationCombo.getSelectedItem();
        }
        return res.getImage((String)imageValue.getSelectedItem());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        key = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        isString = new javax.swing.JRadioButton();
        isImage = new javax.swing.JRadioButton();
        isBoolean = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        stringValue = new javax.swing.JTextField();
        imageValue = new javax.swing.JComboBox();
        booleanValue = new javax.swing.JCheckBox();
        isNavigation = new javax.swing.JRadioButton();
        navigationCombo = new javax.swing.JComboBox();

        FormListener formListener = new FormListener();

        jLabel1.setText("Key");
        jLabel1.setName("jLabel1"); // NOI18N

        key.setName("key"); // NOI18N

        jLabel2.setText("Type");
        jLabel2.setName("jLabel2"); // NOI18N

        buttonGroup1.add(isString);
        isString.setSelected(true);
        isString.setText("String");
        isString.setName("isString"); // NOI18N
        isString.addActionListener(formListener);

        buttonGroup1.add(isImage);
        isImage.setText("Image");
        isImage.setName("isImage"); // NOI18N
        isImage.addActionListener(formListener);

        buttonGroup1.add(isBoolean);
        isBoolean.setText("Boolean");
        isBoolean.setName("isBoolean"); // NOI18N
        isBoolean.addActionListener(formListener);

        jLabel3.setText("Value");
        jLabel3.setName("jLabel3"); // NOI18N

        stringValue.setName("stringValue"); // NOI18N

        imageValue.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        imageValue.setEnabled(false);
        imageValue.setName("imageValue"); // NOI18N

        booleanValue.setEnabled(false);
        booleanValue.setName("booleanValue"); // NOI18N

        buttonGroup1.add(isNavigation);
        isNavigation.setText("Navigation");
        isNavigation.setName("isNavigation"); // NOI18N
        isNavigation.addActionListener(formListener);

        navigationCombo.setEnabled(false);
        navigationCombo.setName("navigationCombo"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(jLabel2)
                    .add(jLabel3))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(booleanValue)
                    .add(key, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(isString)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(isImage)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(isBoolean)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(isNavigation)
                        .add(11, 11, 11))
                    .add(stringValue, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
                    .add(imageValue, 0, 333, Short.MAX_VALUE)
                    .add(navigationCombo, 0, 333, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(key, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(isString)
                    .add(isImage)
                    .add(isBoolean)
                    .add(isNavigation))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(stringValue, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(imageValue, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(navigationCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(booleanValue)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == isString) {
                HashtableKeyValueEditor.this.isStringActionPerformed(evt);
            }
            else if (evt.getSource() == isImage) {
                HashtableKeyValueEditor.this.isImageActionPerformed(evt);
            }
            else if (evt.getSource() == isBoolean) {
                HashtableKeyValueEditor.this.isBooleanActionPerformed(evt);
            }
            else if (evt.getSource() == isNavigation) {
                HashtableKeyValueEditor.this.isNavigationActionPerformed(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    private void isStringActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_isStringActionPerformed
        booleanValue.setEnabled(false);
        stringValue.setEnabled(true);
        imageValue.setEnabled(false);
        navigationCombo.setEnabled(false);
        key.setEnabled(true);
    }//GEN-LAST:event_isStringActionPerformed

    private void isImageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_isImageActionPerformed
        booleanValue.setEnabled(false);
        stringValue.setEnabled(false);
        navigationCombo.setEnabled(false);
        key.setEnabled(true);
        imageValue.setEnabled(true);
    }//GEN-LAST:event_isImageActionPerformed

    private void isBooleanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_isBooleanActionPerformed
        booleanValue.setEnabled(true);
        stringValue.setEnabled(false);
        key.setEnabled(true);
        navigationCombo.setEnabled(false);
        imageValue.setEnabled(false);
    }//GEN-LAST:event_isBooleanActionPerformed

    private void isNavigationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_isNavigationActionPerformed
        booleanValue.setEnabled(false);
        stringValue.setEnabled(false);
        key.setEnabled(false);
        key.setText("$navigation");
        imageValue.setEnabled(false);
        navigationCombo.setEnabled(true);
    }//GEN-LAST:event_isNavigationActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox booleanValue;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox imageValue;
    private javax.swing.JRadioButton isBoolean;
    private javax.swing.JRadioButton isImage;
    private javax.swing.JRadioButton isNavigation;
    private javax.swing.JRadioButton isString;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField key;
    private javax.swing.JComboBox navigationCombo;
    private javax.swing.JTextField stringValue;
    // End of variables declaration//GEN-END:variables

}
